package ${model.packageName}.${model.moduleName}.web;

<#if model.supportImport || model.supportExport>
import com.young.common.core.excel.ExcelExport;
import com.young.common.core.excel.ExcelImport;
import com.young.common.core.excel.IExcelExport;
import com.young.common.core.excel.IExcelImport;
</#if>
import com.young.common.core.base.BaseController;
import com.young.common.util.CommonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.common.util.Page;
<#if model.logService == "1" || model.logService == "2">
import com.young.interfaces.log.annotation.Log;
</#if>
import ${model.packageName}.${model.moduleName}.model.${model.className};
import ${model.packageName}.${model.moduleName}.service.I${model.className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ${model.tableRemark!}服务
 * Created by ${model.author!} on ${model.createTime?date}.
 */
@Controller
@RequestMapping("/${model.moduleName}/${model.className?uncap_first}")
public class ${model.className}Controller extends BaseController {

    @Autowired
    private I${model.className}Service service;

    /**
     * 获取数据列表
     * @param obj
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("查询${model.tableRemark!}列表")
    </#if>
    @RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result${r"<"}List${r"<"}${model.className}${r">>"} getList(${model.className} obj, HttpServletRequest request) {
        List<${model.className}> list = service.getList(obj);
        return Result.buildSuccess(list);
    }

    /**
     * 获取分页数据
     * @param obj
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("分页查询${model.tableRemark!}列表")
    </#if>
    @RequestMapping(value = "/getPage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Page<${model.className}> getPage(${model.className} obj, HttpServletRequest request) {
        Page page = CommonUtil.getPageFromRequest(request);
        List<${model.className}> list = service.getPage(obj, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("查询${model.tableRemark!}")
    </#if>
    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result<${model.className}> get(${primaryKeyType} id, HttpServletRequest request) {
        ${model.className} obj = service.get(id);
        return Result.buildSuccess(obj);
    }

    /**
     * 统计数量
     * @param obj
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("统计${model.tableRemark!}数量")
    </#if>
    @RequestMapping(value = "/count", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result${r"<"}Integer${r">"} count(${model.className} obj, HttpServletRequest request) {
        return Result.buildSuccess(service.count(obj));
    }

    /**
     * 新增
     * @param obj
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("新增${model.tableRemark!}")
    </#if>
    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    @ResponseBody
    public Result insert(${model.className} obj, HttpServletRequest request) {
    <#if model.supportFillBaseDO>
        //插入填充
        this.fillForInsert(obj);
    </#if>
        service.insert(obj);
        return Result.buildSuccess();
    }

    /**
     * 修改
     * @param obj
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("修改${model.tableRemark!}")
    </#if>
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public Result update(${model.className} obj, HttpServletRequest request) {
    <#if model.supportFillBaseDO>
        //更新填充
        this.fillForUpdate(obj);
    </#if>
        service.update(obj);
        return Result.buildSuccess();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("删除${model.tableRemark!}")
    </#if>
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public Result delete(String ids, ${primaryKeyType} id, HttpServletRequest request) {
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
        <#if primaryKeyType == "String">
            service.delete(idArr);
        } else if(StringUtils.isNotBlank(id)) {
        <#else>
            ${primaryKeyType}[] idArrNew = Arrays.stream(idArr).map(${primaryKeyType}::valueOf).toArray(${primaryKeyType}[]::new);
            service.delete(idArrNew);
        } else if(id != null) {
        </#if>
            service.delete(id);
        } else {
            return Result.buildFail("删除失败:待删除编号无效!");
        }
        return Result.buildSuccess();
    }

<#if model.supportImport>
    /**
     * 导入
     * @param file
     * @param request
     * @return
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("导入${model.tableRemark!}")
    </#if>
    @RequestMapping(value = "/import", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Result importExcel(MultipartFile file, HttpServletRequest request) {
        try {
            if (file != null && !file.isEmpty()) {
                //excel导入处理,返回excel中的数据集合
                IExcelImport ei = new ExcelImport(file);//将文件转为ExcelImport对象
                //从excel读取数据
                List<${model.className}> list = ei.getImportDataAsBean(${model.className}.class);
                if (list != null && list.size() > 0){
                    int num = service.batchInsert(list);//批量插入
                    return Result.buildSuccess("成功导入数据" + num + "条!");
                }else {
                    return Result.buildFail("导入失败:excel解析后结果为空!");
                }
            } else {
                return Result.buildFail("导入失败:文件为空!");
            }
        } catch (Exception e) {
            return Result.buildFail("导入失败!");
        }
    }

    /**
     * 下载${model.tableRemark!}excel导入模板
     * @param request
     * @param response
     * @throws Exception
     */
<#if model.logService == "1" || model.logService == "2">
    @Log("下载${model.tableRemark!}excel导入模板")
</#if>
    @RequestMapping(value = "/templateDownload", method = {RequestMethod.GET, RequestMethod.POST})
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        IExcelExport ee = new ExcelExport();
        ee.insertBeanList(new ArrayList(), ${model.className}.class);
        ee.write2Response(response, "template.xls");
    }

</#if>
<#if model.supportExport>
    /**
     * 导出
     * @param obj 查询参数
     * @param request
     * @param response
     * @throws Exception
     */
    <#if model.logService == "1" || model.logService == "2">
    @Log("导出${model.tableRemark!}")
    </#if>
    @RequestMapping(value = "/export", method = {RequestMethod.GET, RequestMethod.POST})
    public void exportExcel(${model.className} obj, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<${model.className}> list = service.getList(obj);
        if (list != null && list.size() > 0){
            IExcelExport ee = new ExcelExport();
            ee.insertBeanList(list, ${model.className}.class);
            ee.write2Response(response, "excel_" + System.currentTimeMillis() + ".xls");
        }else{
            response.getWriter().write("数据为空!");
        }
    }

</#if>
}